package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatGameServerResponseParams;
import com.foolishbird.sdk.params.RedSendResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.FloatGameServer;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.utils.EncodingUtils;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;

import java.util.List;


public class RedpackageQRCodeFragment extends BaseHttpFragment implements View.OnClickListener {



    private TextView titleTxt;
    private ImageView qrView,backView;
    private RedSendResponseParams.RedSendResponseData data;
    private Handler myHander;

    public static final RedpackageQRCodeFragment newInstance(Bundle bundle) {
        RedpackageQRCodeFragment fragment = new RedpackageQRCodeFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_redpackage_qrcode");
        View contentView =inflater.inflate(resource,null);
        titleTxt = contentView.findViewById(getViewIdentifier("redpackage_qrcode_money"));
        titleTxt.setText("￥"+getArguments().getString("money"));
        qrView = contentView.findViewById(getViewIdentifier("redpackage_qrcode_image"));
        backView = contentView.findViewById(getViewIdentifier("redpackage_open_close"));
        data = (RedSendResponseParams.RedSendResponseData)getArguments().getSerializable("data");
        int width = ResUtils.dip2px(getContext(),104);

        qrView.setImageBitmap(EncodingUtils.createQRCode(data.getPayStr(),width,width,null));
        backView.setOnClickListener(this);
//        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
//        titleTxt.setText("发红包");
        myHander = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
//                WL.d("RedpackageQRCodeFragment",RedpackageQRCodeFragment.this.i);
                if(myHander!=null&&getContext()!=null){
                    taskid =HttpEngineApi.getInstance().redpackageCheck(data.getOrderNo(),requestCallbackHandler(FloatGameServerResponseParams.class));
                }else {
                    myHander.removeMessages(1);
                }
            }
        };

        myHander.sendEmptyMessageDelayed(1,1000);
        return contentView;
    }

    private int  taskid;

    @Override
    public void onStop() {
        super.onStop();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(myHander!=null) myHander.removeMessages(1);
        WL.d("BaseHttpFragmentRedpackageQRCodeFragment","ondestroy");
    }

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if(flag == taskid){
                    Toast.makeText(getContext(),"发送成功",Toast.LENGTH_SHORT).show();;
                    myHander.removeMessages(1);
                    MyFragmentManger.getInstance().goBack(this);
//                    MyFragmentManger.getInstance().goBack();
                }
            }
            else{
                myHander.sendEmptyMessageDelayed(1,1000);
//                if(n<3){
//                    n++;
//                    myHander.sendEmptyMessageDelayed(1,1000);
//                }else{
//                    Toast.makeText(getContext(),"发送成功",Toast.LENGTH_SHORT).show();;
//                    myHander.removeMessages(1);
//                    MyFragmentManger.getInstance().goBack(this);
//                }
//                MyFragmentManger.getInstance().goBack(this);
//                MyFragmentManger.getInstance().goBack();

            }
//            setAdapter();
        }



        WL.d("FloatNoticeFragment",response.toString());
    }

//    int n=0;



    @Override
    public void onClick(View view) {
        if(view ==backView){
            MyFragmentManger.getInstance().goBack(this,true);
            myHander.removeMessages(1);myHander=null;
        }
//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }

    private OnFinishActivityListener getOnFinishActivityListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnFinishActivityListener) {
                return (OnFinishActivityListener) activity;
            }
        }
        return null;
    }
}
